/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package me.joshua.tools4j.web;

import java.util.List;

import me.joshua.tools4j.dal.CheckPointRepository;
import me.joshua.tools4j.dal.SubscriptionRepository;
import me.joshua.tools4j.model.Subscription;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * 
 * @author Joshua
 */
@Controller
@RequestMapping("/subscription")
public class SubscriptionController {

	@Autowired
	private CheckPointRepository	checkPointRepository;
	@Autowired
	private SubscriptionRepository	subscriptionRepository;

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public @ResponseBody
	Subscription get(@PathVariable("id") Subscription subscription) {
		return subscription;
	}

	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable("id") Subscription subscription, Model model) {
		model.addAttribute("subscription", subscription);
		if (null != subscription) {
			model.addAttribute("points", checkPointRepository.findBySubscriptionAndEnabledOrderByOrderAsc(subscription, true));
		}
		return "/page/subscription/detail";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("isNew", true);
		Subscription subscription = new Subscription();
		return edit(subscription, model);
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable("id") Subscription subscription, Model model) {
		model.addAttribute("subscription", subscription);
		return "/page/subscription/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(Subscription subscription) {
		subscriptionRepository.save(subscription);
		return "redirect:" + UriComponentsBuilder.fromPath("/subscription/edit/{id}").buildAndExpand(subscription.getId());
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<Subscription> subscriptions = subscriptionRepository.findByEnabled(true);
		model.addAttribute("subscriptions", subscriptions);
		return "/page/subscription/list";
	}

	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody
	Subscription del(@PathVariable("id") Subscription subscription) {
		if (null == subscription) {
			return null;
		}
		subscriptionRepository.delete(subscription);
		return subscription;
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test() {
		return "/page/subscription/test";
	}

}
